package ru.mcfr.oxygen.util.web;

import org.w3c.dom.Document;
import ru.mcfr.oxygen.util.FillIDs;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 05.05.11
 * Time: 13:38
 * To change this template use File | Settings | File Templates.
 */
public class TypesAndSchemasInfoGetter {

    private String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n" +
            "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +
            "    xmlns:xd=\"http://www.oxygenxml.com/ns/doc/xsl\"\n" +
            "    exclude-result-prefixes=\"xs xd\"\n" +
            "    version=\"2.0\">\n" +
            "    <xsl:output indent=\"yes\"></xsl:output>\n" +
            "    <xsl:template match=\"/\">\n" +
            "        <root>\n" +
            "            <xsl:apply-templates select=\"//schema\"/>\n" +
            "        </root>\n" +
            "    </xsl:template>\n" +
            "    <xsl:template match=\"schema\">\n" +
            "        <xsl:copy>\n" +
            "            <xsl:copy-of select=\"@*\"/>\n" +
            "            <xsl:copy-of select=\"//type[@noNamespaceSchemaLocation eq current()/@noNamespaceSchemaLocation]\"></xsl:copy-of>\n" +
            "        </xsl:copy>\n" +
            "    </xsl:template>\n" +
            "</xsl:stylesheet>";
    private AbstractWebConnector awc;
    private Properties p;

    private Logger logger = Logger.getLogger(TypesAndSchemasInfoGetter.class.getName());

    public TypesAndSchemasInfoGetter(Properties props) {
        p = props;
        awc = new AbstractWebConnector(props);
    }

    public Document getInfo() {
        FillIDs xmlEr = new FillIDs();
//        String xml = "<root>";
//        xml += awc.getFromURL(p.getProperty("url-to-docTypes"));
//        xml += awc.getFromURL(p.getProperty("url-to-schemas"));
//        xml += "</root>";
//        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");

        String pack = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<root title=\"Схемы и типы\" id=\"1\" noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\">\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"1\"\n" +
                "           title=\"Нормативный_акт\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\">\n" +
                "      <type id=\"86\" title=\"Кодекс\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"115\" title=\"Постановление\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"116\" title=\"Приказ\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"117\" title=\"Письмо\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"118\" title=\"Закон\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"119\" title=\"ФКЗ\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"120\" title=\"ФЗ\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"121\" title=\"Конституция\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"122\" title=\"Указ\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"123\" title=\"Распоряжение\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"124\" title=\"Рекомендации\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"125\" title=\"Указание\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"130\" title=\"Декларация\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"131\" title=\"Пакт\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"132\" title=\"Конвенция\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"133\" title=\"Договор\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"134\" title=\"Локальный акт (авторский)\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"136\" title=\"Соглашение\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"137\" title=\"Положение\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"138\" title=\"Устав\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"139\" title=\"Решение\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"140\" title=\"Программа\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"142\" title=\"Перечень\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"143\" title=\"Регламент\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"144\" title=\"Разъяснение\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"145\" title=\"Инструкция\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "      <type id=\"146\" title=\"Руководство\" schema=\"1\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=1\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"2\" title=\"Вопрос_ответ\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=2\">\n" +
                "      <type id=\"84\" title=\"Вопрос-ответ\" schema=\"2\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=2\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"3\" title=\"Статьи\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=3\">\n" +
                "      <type id=\"88\" title=\"Обзорная статья\" schema=\"3\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=3\"/>\n" +
                "      <type id=\"128\" title=\"Комментарий\" schema=\"3\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=3\"/>\n" +
                "      <type id=\"80\" title=\"Аналитическая статья\" schema=\"3\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=3\"/>\n" +
                "      <type id=\"135\" title=\"Аналитическая таблица\" schema=\"3\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=3\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"4\" title=\"Новость\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=4\">\n" +
                "      <type id=\"79\" title=\"Новость\" schema=\"4\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=4\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"5\" title=\"Обзор_шаблона\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=5\">\n" +
                "      <type id=\"83\" title=\"Обзор шаблона\" schema=\"5\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=5\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"6\"\n" +
                "           title=\"Пошаговая_инструкция\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=6\">\n" +
                "      <type id=\"81\" title=\"Пошаговая инструкция\" schema=\"6\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=6\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"7\"\n" +
                "           title=\"Анонимное решение суда\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=7\">\n" +
                "      <type id=\"151\" title=\"Анонимное решение суда\" schema=\"7\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=7\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"8\" title=\"Термин\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=8\">\n" +
                "      <type id=\"82\" title=\"Термин\" schema=\"8\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=8\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"9\" title=\"Юриспруденция\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=9\">\n" +
                "      <type id=\"126\" title=\"Определение суда\" schema=\"9\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=9\"/>\n" +
                "      <type id=\"127\" title=\"Решение суда\" schema=\"9\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=9\"/>\n" +
                "      <type id=\"129\" title=\"Постановление суда\" schema=\"9\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=9\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"11\"\n" +
                "           title=\"Алгоритм пошаговой инструкции\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=11\">\n" +
                "      <type id=\"147\" title=\"Алгоритм пошаговой инструкции\" schema=\"11\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=11\"/>\n" +
                "   </schema>\n" +
                "   <schema xmlns:exist=\"http://exist.sourceforge.net/NS/exist\" id=\"12\"\n" +
                "           title=\"Обзор журнальной статьи\"\n" +
                "           noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=12\">\n" +
                "      <type id=\"149\" title=\"Обзор журнальной статьи\" schema=\"12\"\n" +
                "            noNamespaceSchemaLocation=\"http://10.0.64.221/control/get_schema_by_id/?s=12\"/>\n" +
                "   </schema>\n" +
                "</root>";
//        try {
//            pack = myTransformer(xml,xsl);
//        } catch (TransformerException e) {
//            e.printStackTrace();
//        }

        return xmlEr.stringXMLtoDocument(pack);


    }

    public static String myTransformer(String sourceID, String xslID)
            throws TransformerException, TransformerConfigurationException {

        // Create a transform factory instance.
        TransformerFactory tfactory = TransformerFactory.newInstance();

        // Create a transformer for the stylesheet.
        Transformer transformer = tfactory.newTransformer(new StreamSource(xslID));

        // Transform the source XML to System.out.
        String result = "";
        transformer.transform(new StreamSource(sourceID), new StreamResult(result));
        return result;
    }
}
